using System;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;

namespace Yoozoo.Gameplay.City
{
    [Serializable]
    public class CityBuildingFloorData
    {
        
    }
    
    //建筑外围一圈单格的数据
    [Serializable]
    public class CityBuildingFloorCellData
    {
        //地板的坐标
        public int x;
        public int y;
        //地板的类型
        public  int type;
        //地板id
        public int floorId;
        //装饰物信息
        public CityBuildingFloorDecorationData decorationData;
    }
    
    //建筑外围一圈单格的装饰物数据
    [Serializable]
    public class CityBuildingFloorDecorationData
    {
        //装饰物的类型
        public string name;
       //处理连接时，是否需要隐藏
        public bool needHide;
    }
    
    public class CityBuildingFloorEditor : MonoBehaviour
    {
        //[Header("策划配置json文件路径")]
        //public string eventInfoConfigPath;
        [Header("建筑的id")]
        public int id;
        [Header("建筑的尺寸(扩大之后的尺寸)")]
        public Vector2 size;
        [Header("建筑的格子数据")]
        public List<CityBuildingFloorCellData> cells;
        
        private Dictionary<int, Dictionary<int,string>> tempDecorateDic;

        //根据尺寸刷新格子数据(建筑周围一圈)
        public void RefreshCells()
        {
            tempDecorateDic = new Dictionary<int, Dictionary<int, string>>();
            //检查节点下面所有的装饰，根据位置判断自动所在的格子
            for (int i = 0; i < transform.childCount; i++)
            {
                Transform childTra = transform.GetChild(i);
                Vector3 pos = childTra.localPosition;
                int x = (int)math.floor(-pos.z / 2.5f) ;
                int y = (int)math.floor(pos.x / 2.5f) ;
                if (!tempDecorateDic.ContainsKey(x))
                {
                    tempDecorateDic[x] = new Dictionary<int, string>();
                }
                tempDecorateDic[x][y] = childTra.name;
                UnityEngine.Debug.Log("地板上的装饰坐标：X:"+x+"Y:"+y);
            }
            //获取建筑的格子数据
            cells = new List<CityBuildingFloorCellData>();
            //总数据量是y*2 + x*2 - 4
            //遍历建筑的格子
            for (int i = -1; i < size.x - 1; i++)
            {
                for (int j = -1; j < size.y-1; j++)
                {
                    if (i == -1 || i == size.x - 2 || j == -1 || j == size.y - 2)
                    {
                        //获取格子的数据
                        var cellData = new CityBuildingFloorCellData();
                        cellData.x = i;
                        cellData.y = j;
                        //TODO:地板类型，后续如果要多种地板从这里扩展
                        cellData.type = 1;
                        //初始地板的id是固定的
                        //左上：12，上：14，右上：6，左：13，右：7，左下：9，下：11，右下：3
                        if (i == -1)
                        {
                            if (j == -1)
                            {
                                cellData.floorId = 12;
                            }
                            else if (j == size.y - 2)
                            {
                                cellData.floorId = 6;
                            }
                            else
                            {
                                cellData.floorId = 14;
                            }
                        }
                        else if (i == size.x - 2)
                        {
                            if (j == -1)
                            {
                                cellData.floorId = 9;
                            }
                            else if (j == size.y - 2)
                            {
                                cellData.floorId = 3;
                            }
                            else
                            {
                                cellData.floorId = 11;
                            }
                        }
                        else
                        {
                            if (j == -1)
                            {
                                cellData.floorId = 13;
                            }
                            else if (j == size.y - 2)
                            {
                                cellData.floorId = 7;
                            }
                        }
                        
                        cellData.decorationData = new CityBuildingFloorDecorationData();
                        if (tempDecorateDic.ContainsKey(i) && tempDecorateDic[i].ContainsKey(j))
                        {
                            cellData.decorationData.name = tempDecorateDic[i][j];
                        }
                        else
                        {
                            cellData.decorationData.name = "";
                        }
                        
                        cellData.decorationData.needHide = false;
                        //添加格子数据
                        cells.Add(cellData);
                    }
                }
            }

            
            
        }
        
        public List<CityBuildingFloorCellData> GetBuildingFloorData()
        {
            return cells;
        }
        
    }
    
 
}
